package com.jie.eduorder.service.impl;

import com.jie.commonutils.ordervo.CourseWebVOOrder;
import com.jie.commonutils.ordervo.UcenterMemberOrder;
import com.jie.eduorder.client.EduClient;
import com.jie.eduorder.client.UcenterClient;
import com.jie.eduorder.entity.Order;
import com.jie.eduorder.mapper.OrderMapper;
import com.jie.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xzj
 * @since 2020-10-17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private final EduClient eduClient;

    private final UcenterClient ucenterClient;

    @Autowired
    public OrderServiceImpl(
            EduClient eduClient,
            UcenterClient ucenterClient
    ) {
        this.eduClient = eduClient;
        this.ucenterClient = ucenterClient;
    }

    @Override
    public String createOrder(String courseId, String memberId) {
        // 通过远程调用获取用户信息(根据用户id)
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

        // 通过远程调用获取课程信息(根据课程id)
        CourseWebVOOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        // 创建Order对象, 向Order对象中设置需要的数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());              // 订单号
        order.setCourseId(courseId);                             // 课程id
        order.setCourseTitle(courseInfoOrder.getTitle());        // 课程标题
        order.setCourseCover(courseInfoOrder.getCover());        // 课程封面
        order.setTeacherName(courseInfoOrder.getTeacherName());  // 讲师名
        order.setTotalFee(courseInfoOrder.getPrice());           // 课程价格
        order.setMemberId(memberId);                             // 用户id
        order.setMobile(userInfoOrder.getMobile());              // 用户手机号
        order.setNickname(userInfoOrder.getNickname());          // 用户昵称
        order.setStatus(0);                                      // 支付状态(0: 未支付; 1: 已支付)
        order.setPayType(1);                                     // 支付类型(1代表微信支付)

        // 奇怪, 要是微信登录的用户要怎么支付啊, 都没有手机号这些(不过还好, 订单中有用户id)

        baseMapper.insert(order);

        // 返回订单号
        return order.getOrderNo();
    }

}
